package com.party.admin.biz.openPlatform;

import com.alibaba.fastjson.JSONObject;
import com.party.api.dto.wechat.requst.MenuButtonRequest;
import com.party.api.dto.wechat.response.GetMenuResponse;
import com.party.api.dto.wechatPlatform.response.WechatAuthorizerInfoResponse;
import com.party.api.dto.wechatPlatform.response.WechatAuthorizerUserInfoResponse;
import com.party.api.service.wechat.IWechatService;
import com.party.common.constant.Constant;
import com.party.core.model.member.DeployType;
import com.party.core.model.member.MemberExternalApp;
import com.party.core.model.wechat.WechatMenu;
import com.party.core.model.wechat.WechatMenuActionType;
import com.party.core.model.wechatPlatform.WechatOpenMember;
import com.party.core.service.member.IMemberExternalAppService;
import com.party.core.service.wechat.IWechatMenuService;
import com.party.core.service.wechatPlatform.IWechatOpenMemberService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 开放平台的第三方平台授权业务
 */
@Service
public class AuthorizationBizService {

    protected static Logger logger = LoggerFactory.getLogger(AuthorizationBizService.class);
    @Autowired
    private IWechatOpenMemberService wechatOpenMemberService;

    @Autowired
    private IWechatService wechatService;

    @Autowired
    private IWechatMenuService wechatMenuService;

    @Autowired
    private IMemberExternalAppService memberExternalAppService;

    /**
     * 保存接口调用凭据和授权信息
     *
     * @param authorizationInfo
     * @param memberId
     * @return
     */
    public WechatOpenMember saveAuthorizerInfo(WechatAuthorizerInfoResponse authorizationInfo, String memberId) {

        //根据appId查询，没有则新增。mvn
        WechatAuthorizerInfoResponse.AuthorizationInfo authorization_info = authorizationInfo.getAuthorization_info();
        WechatOpenMember wechatOpenMember
                = wechatOpenMemberService.findByAppidAndDeployType(authorization_info.getAuthorizer_appid(),
                DeployType.AUTHORIZATION.getCode());
        if (null == wechatOpenMember){
            wechatOpenMember = new WechatOpenMember();
            wechatOpenMember.setAuthorizerAppid(authorization_info.getAuthorizer_appid());
            wechatOpenMember.setAuthorizerAccessToken(authorization_info.getAuthorizer_access_token());
            wechatOpenMember.setAuthorizerRefreshToken(authorization_info.getAuthorizer_refresh_token());
            wechatOpenMember.setType("PARTNER");
            wechatOpenMemberService.insert(wechatOpenMember);
        }
        else {
            wechatOpenMember.setAuthorizerAppid(authorization_info.getAuthorizer_appid());
            wechatOpenMember.setAuthorizerAccessToken(authorization_info.getAuthorizer_access_token());
            wechatOpenMember.setAuthorizerRefreshToken(authorization_info.getAuthorizer_refresh_token());
            wechatOpenMemberService.update(wechatOpenMember);
        }

        //保存分配关系
        MemberExternalApp memberExternalApp
                = memberExternalAppService.getSet(memberId, DeployType.AUTHORIZATION.getCode());
        memberExternalApp.setExternalAccountId(wechatOpenMember.getId());
        memberExternalAppService.update(memberExternalApp);
        try {
            synchronousMenu(wechatOpenMember.getAuthorizerAccessToken(), memberId);
        } catch (Exception e) {
            logger.error("同步菜单信息异常", e);
        }
        return wechatOpenMember;
    }

    /**
     * 保存授权方的帐号基本信息
     *
     * @param authorizerInfo 授权者信息
     * @param openMember 开放平台信息
     * @return 开放平台信息
     */
    public WechatOpenMember saveAuthorizerBaseInfo(WechatAuthorizerUserInfoResponse authorizerInfo,
                                                   WechatOpenMember openMember) {

        // 授权信息
        WechatAuthorizerUserInfoResponse.AuthorizationInfo authorization_info = authorizerInfo.getAuthorization_info();

        // 授权者信息
        WechatAuthorizerUserInfoResponse.AuthorizerUserInfo authorizer_info = authorizerInfo.getAuthorizer_info();

        //授权方昵称
        openMember.setNickName(authorizer_info.getNick_name());

        //授权方头像
        openMember.setHeadImg(authorizer_info.getHead_img());

        //授权方公众号类型
        openMember.setServiceTypeInfo(authorizer_info.getService_type_info().getId());

        //授权方认证类型
        openMember.setVerifyTypeInfo(authorizer_info.getVerify_type_info().getId());

        //授权方公众号的原始ID
        openMember.setUserName(authorizer_info.getUser_name());

        //公众号的主体名称
        openMember.setPrincipalName(authorizer_info.getPrincipal_name());

        //授权方公众号所设置的微信号
        openMember.setAlias(authorizer_info.getAlias());
        openMember.setBusiness_info(JSONObject.toJSONString(authorizer_info.getBusiness_info()));

        //二维码图片的URL
        openMember.setQrcodeUrl(authorizer_info.getQrcode_url());

        // 账号介绍
        openMember.setSignature(authorizer_info.getSignature());

        //授权集合
        openMember.setFuncInfo(JSONObject.toJSONString(authorization_info.getFunc_info()));

        wechatOpenMemberService.update(openMember);
        return openMember;
    }

    /**
     * 同步菜单
     * @param accessToken 公众号交互令牌
     */
    @Transactional
    public void synchronousMenu(String accessToken, String memberId){
        wechatMenuService.deleteByMemberId(memberId);
        GetMenuResponse getMenuResponse = wechatService.getMenu(accessToken);
        if (getMenuResponse.getMenuRequest() != null && getMenuResponse.getMenuRequest().getButton() != null){
            treeToList(getMenuResponse.getMenuRequest().getButton(), Constant.TOP_PARENT_ID, memberId);
        }
    }


    /**
     * 树转list
     * @param list 树结构
     * @param parentId 父级编号
     */
    public void treeToList(List<MenuButtonRequest> list, String parentId, String memberId){
        int index = 0;
        for (MenuButtonRequest menuButtonRequest : list){
            WechatMenu wechatMenu = new WechatMenu();
            wechatMenu.setName(menuButtonRequest.getName());
            wechatMenu.setMemberId(memberId);
            if (WechatMenuActionType.CLICK.getValue().equals(menuButtonRequest.getType())){
                wechatMenu.setActionType(WechatMenuActionType.CLICK.getCode());
            }
            if (WechatMenuActionType.VIEW.getValue().equals(menuButtonRequest.getType())){
                wechatMenu.setActionType(WechatMenuActionType.VIEW.getCode());
                wechatMenu.setUrl(menuButtonRequest.getUrl());
            }
            if("media_id".equals(menuButtonRequest.getType())){
                wechatMenu.setActionType(WechatMenuActionType.NEWS.getCode());
                wechatMenu.setMediaId(menuButtonRequest.getMediaId());
            }
            wechatMenu.setParentId(parentId);
            wechatMenu.setSort(index);
            index++;
            wechatMenuService.insert(wechatMenu);
            if (!CollectionUtils.isEmpty(menuButtonRequest.getButtonRequestList())){
                treeToList(menuButtonRequest.getButtonRequestList(), wechatMenu.getId(), memberId);
            }
        }
    }

}
